package com.ruicar.afs.cloud.afscase.mq.receiver.processor;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruicar.afs.cloud.afscase.common.utils.Const;
import com.ruicar.afs.cloud.afscase.infomanagement.entity.CaseBaseInfo;
import com.ruicar.afs.cloud.afscase.infomanagement.service.CaseBaseInfoService;
import com.ruicar.afs.cloud.afscase.processor.service.ApproveWorkflowService;
import com.ruicar.afs.cloud.common.core.enums.AfsEnumUtil;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.BusinessStateInEnum;
import com.ruicar.afs.cloud.common.modules.afscorebusiness.enums.ResultBooleanEnum;
import com.ruicar.afs.cloud.common.modules.dto.mq.approve.CallBackApplyResultDto;
import com.ruicar.afs.cloud.common.mq.rabbit.listener.AfsMqBizProcessor;
import com.ruicar.afs.cloud.common.mq.rabbit.message.MqTransCode;
import com.ruicar.afs.cloud.workflow.api.dto.flowprocess.WorkFlowResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>Description：</p>
 * 审批撤回结果MQ消息接收处理类
 * @author ma_xueyu
 * @version 1.0
 * @createDate 2020-09-15
 */
@AllArgsConstructor
@Slf4j
@Component
public class CallBackResultProcessor implements AfsMqBizProcessor<CallBackApplyResultDto> {

    private final CaseBaseInfoService caseBaseInfoService;
    private ApproveWorkflowService approveWorkflowService;

    @Override
    public boolean processMessage(CallBackApplyResultDto callBackResultDto) throws Exception {
        this.CallBackApplyQueue(callBackResultDto);
        return true;
    }

    @Override
    public MqTransCode getCode() {
        return MqTransCode.AFS_POS_APPLY_CASE_CTM_APPROVE_CALL_BACK;
    }

    @Transactional(rollbackFor = Exception.class)
    public void CallBackApplyQueue(CallBackApplyResultDto callBackResultDto) throws Exception {
        log.info("接收审批撤回结果MQ消息："+ JSON.toJSONString(callBackResultDto));
        //经销商端可撤回，执行工作流撤回接口
        if(AfsEnumUtil.key(ResultBooleanEnum.TRUE).equals(callBackResultDto.getResult())){
            CaseBaseInfo caseBaseInfo = caseBaseInfoService.getOne(Wrappers.<CaseBaseInfo>query().lambda()
                    .eq(CaseBaseInfo::getApplyNo, callBackResultDto.getApplyNo()));
            String callbackRemark = "【"+callBackResultDto.getCallBackUserRealName()+"主动撤回】";
            if(AfsEnumUtil.key(BusinessStateInEnum.REVISE_PARSE).equals(caseBaseInfo.getBusinessStateIn())){
                WorkFlowResponse response = approveWorkflowService.callBackFlow(callBackResultDto.getScheduleId(),
                        callbackRemark,callBackResultDto.getCallbackUsername(),callBackResultDto.getCallBackUserRealName());
                if (Const.WORKFLOW_RESPONSE_SUCCESS == response.getSuccess()) {
                    log.info("{}状态下审批撤回成功，申请编号：{}",AfsEnumUtil.desc(BusinessStateInEnum.REVISE_PARSE),callBackResultDto.getApplyNo());
                }else{
                    log.info("{}状态下审批撤回失败，申请编号：{}",AfsEnumUtil.desc(BusinessStateInEnum.REVISE_PARSE),callBackResultDto.getApplyNo());
                }
            }
        }else{
            log.info("{}状态下审批撤回失败，申请编号：{}",AfsEnumUtil.desc(BusinessStateInEnum.REVISE_PARSE),callBackResultDto.getApplyNo());
        }
    }
}
